package server;

import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

import job.Job;

public class RandomizedThread extends Thread {
	
	private RandomizedServer server;
	private Job job;
	private Queue replyTo;
	

	public RandomizedThread(RandomizedServer server, Job job, Queue replyTo) {
		this.server = server;
		this.job = job;
		this.replyTo = replyTo;
	}

	public void run() {
		try {
			QueueSession qs = server.getQueueConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			job.run();
			ObjectMessage reply = qs.createObjectMessage();
			QueueSender qsend = qs.createSender(null);

			reply.setObject(job);
			qsend.send(replyTo, reply);
		} catch(Exception e) {
			e.printStackTrace();
		}
		server.removeJob(job.getName());
	}

}